package au.edu.utas.yu_qiao_group.luckyman.ui.draws;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import au.edu.utas.yu_qiao_group.luckyman.R;
import au.edu.utas.yu_qiao_group.luckyman.databinding.DrawListItemBinding;
import au.edu.utas.yu_qiao_group.luckyman.db.draws.DrawDTO;
import au.edu.utas.yu_qiao_group.luckyman.db.draws.DrawListDTO;
import au.edu.utas.yu_qiao_group.luckyman.ui.components.SwipeRecyclerView;

public class DrawListViewItemAdapter extends SwipeRecyclerView.Adapter<DrawListViewItemAdapter.ViewHolder> {
  private static final String TAG = "DrawListViewItem";

  private List<DrawListDTO> mDataSet;
  private Context mCtx;
  private View.OnClickListener mStartBtnListener, mDelBtnListener;

  public static class ViewHolder extends SwipeRecyclerView.ViewHolder implements SwipeRecyclerView.SwiperViewHolder {
    private DrawListItemBinding mBinding;
    public LinearLayout listRow, listHidden;
    public ImageView mImgView;
    public ImageButton mStartBtn;
    public TextView mItemDelete;

    public ViewHolder(View itemView) {
      super(itemView);
      mBinding = DataBindingUtil.bind(itemView);
      listRow = itemView.findViewById(R.id.draw_list_row);
      listHidden = itemView.findViewById(R.id.list_hidden);
      mImgView = itemView.findViewById(R.id.raffle_cover);
      mStartBtn = itemView.findViewById(R.id.start);
      mItemDelete = itemView.findViewById(R.id.draw_btn_del2);
    }

    public LinearLayout getListRow() {
      return listRow;
    }

    public LinearLayout getListHidden() {
      return listHidden;
    }

    public void bind(DrawListDTO draw) {
      mBinding.setDraw(draw);
    }
  }

  /**
   * Initialize the dataset of the Adapter.
   *
   * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
   */
  public DrawListViewItemAdapter(List<DrawListDTO> dataSet, View.OnClickListener startBtnListener, View.OnClickListener delBtnListener) {
    mDataSet = dataSet;
    mStartBtnListener = startBtnListener;
    mDelBtnListener = delBtnListener;
  }

  // Create new views (invoked by the layout manager)
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    // Create a new view.
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.draw_list_item, viewGroup, false);
    mCtx = viewGroup.getContext();
    return new ViewHolder(v);
  }

  @Override
  public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Log.d(TAG, "Element " + position + " set.");
    DrawListDTO draw = mDataSet.get(position);
    viewHolder.bind(draw);
    switch (draw.getMStatus()) {
      case DrawDTO.STATUS_PENDING:
        Drawable drawDrawable = ResourcesCompat.getDrawable(mCtx.getResources(), R.drawable.ic_play_circle_outline_black_24dp, null);
        viewHolder.mStartBtn.setImageDrawable(drawDrawable);
        viewHolder.mStartBtn.setOnClickListener(mStartBtnListener);
        break;
      case DrawDTO.STATUS_FINISHED:
        Drawable checkDrawable = ResourcesCompat.getDrawable(mCtx.getResources(), R.drawable.ic_comment_black_24dp, null);
        viewHolder.mStartBtn.setImageDrawable(checkDrawable);
        viewHolder.mStartBtn.setOnClickListener(mStartBtnListener);
        break;
      default:
    }
    viewHolder.mStartBtn.setTag(position);
    viewHolder.mItemDelete.setOnClickListener(mDelBtnListener);
    viewHolder.mItemDelete.setTag(position);
  }

  @Override
  public int getItemCount() {
    return mDataSet.size();
  }
}
